package com.bnlr.lrparking.appui.sharevalue.deposit;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.appui.bank.AddBankCardOneActivity;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.BankCardAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.event.AddBankCardSucceeEvent;
import com.bnlr.lrparking.appui.bank.model.BankCardM;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 2017/8/30.
 * 选择银行卡
 */

public class SelectBankCardActivity extends BaseActivity {

    private TextView tvNavTitle;
    private ImageView iv_nav_right;
    private List<BankCardM.DataBean> list=new ArrayList<>();
//    private BankCardM bankCardM=new BankCardM();
    private BankCardAdapter adapter;
    private ListView lv_select_card;
    private LinearLayout noData;
    RelativeLayout relativeLayout ;
    private String isRealNameAuth = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bankcard);
        initView();
        getBandCard();

    }

    private void initView(){
        EventBus.getDefault().register(this);//
        isRealNameAuth=AppConfig.getInstance().getString("isRealNameAuth", "");
        tvNavTitle=(TextView)findViewById(R.id.tv_nav_title);
        tvNavTitle.setText("选择银行卡");
        iv_nav_right=(ImageView)findViewById(R.id.iv_nav_right);
        iv_nav_right.setImageResource(R.mipmap.add_black);
        iv_nav_right.setOnClickListener(this);
        lv_select_card=(ListView)findViewById(R.id.lv_select_card);
        noData = (LinearLayout) findViewById(R.id.no_bank_data);
        relativeLayout = (RelativeLayout) findViewById(R.id.rl_add_bank);
        lv_select_card.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectPosition(position);
                Intent intent=new Intent();
                intent.putExtra("select_banks",list.get(position));
                setResult(1,intent);//发送返回码
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.iv_nav_right://添加
                if (isRealNameAuth.equals("0") || isRealNameAuth.equals("")) {
                    ToastUtils.showShort("请先去身份信息验证");
                }else if (isRealNameAuth.equals("2")){
                    ToastUtils.showShort("身份信息正在审核中");
                }else if (isRealNameAuth.equals("3")){
                    ToastUtils.showShort("身份信息认证失败，请重新认证");
                }else {
                   String realName = AppConfig.getInstance().getString("realName", "");
                    startActivity(new Intent(this, AddBankCardOneActivity.class)
                            .putExtra("real_name", realName));
                }
                break;
        }
    }

    private void getBandCard(){
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "GetUserBank");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));

            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<BankCardM>(baseContext, true, BankCardM.class) {
                        @Override
                        public void doWork(BankCardM data, String code) {
                            if (data.getData().size() != 0) {
                                noData.setVisibility(View.GONE);
                                list = data.getData();
                                adapter = new BankCardAdapter(baseContext, data.getData());
                                lv_select_card.setAdapter(adapter);
                            } else {
                                noData.setVisibility(View.VISIBLE);
                                relativeLayout.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        startActivity(AddBankCardOneActivity.class);
                                    }
                                });

                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                if (!code.equals("1")) {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBankCardSucceeEvent(AddBankCardSucceeEvent event) {
        getBandCard();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
